﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace PIMS.Util
{
    public class XmlDocUtil
    {
        private static XmlDocument xmlDocument;
        private static XmlReader reader;

        public static void LoadXmlDocumet(string xmlFile)
        {
            xmlDocument = new XmlDocument();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;
            reader = XmlReader.Create(xmlFile,settings);
            xmlDocument.Load(reader);
        }

        public static void LoadXmlString(string xmlString)
        {
            xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xmlString);
        }

        public static void Demo1()
        {
            XmlNodeList nodeList = GetXmlNodeListByTagName("PLCServerIp");
            string ServerIp = nodeList[0].Attributes["value"].Value;

        }

        public static XmlNodeList GetXmlNodeListByTagName(string tagName)
        {
            XmlNodeList nodeList = xmlDocument.GetElementsByTagName(tagName);
            return nodeList;
        }

        /// <summary>
        /// glb add
        /// </summary>
        /// <param name="xPath">like "/licensefile/license/details"</param>
        /// <param name="tagName"></param>
        /// <returns></returns>
        public static XmlNodeList GetXmlNodeListByTagName(string xPath,string tagName)
        {
            XmlElement xe = xmlDocument.DocumentElement;
            XmlElement selectXe = (XmlElement)xe.SelectSingleNode(xPath);
            XmlNodeList nodeList = selectXe.GetElementsByTagName(tagName);
            return nodeList;
        }
        public static void ReadFinish()
        {
            if(reader !=null)
            {
                reader.Close();
            }
        }

        public static string CreateResultXML(int flag,string code,string message,string info)
        {
            XmlDocument xmldoc;
            XmlElement xmlelem;

            xmldoc = new XmlDocument();
            //加入XML的声明段落，<?xml version="1.0" encoding="utf-8"?>
            XmlDeclaration xmldecl;
            xmldecl = xmldoc.CreateXmlDeclaration("1.0","utf-8",null);
            xmldoc.AppendChild(xmldecl);

            //加入一个根元素
            xmlelem = xmldoc.CreateElement("","response","");
            xmldoc.AppendChild(xmldecl);
            XmlNode root = xmldoc.SelectSingleNode("response");

            XmlElement eIFlag = xmldoc.CreateElement("flag");
            eIFlag.InnerText = flag.ToString();
            root.AppendChild(eIFlag);

            XmlElement eIfCode = xmldoc.CreateElement("code");
            eIfCode.InnerText = code;
            root.AppendChild(eIfCode);

            XmlElement eIMessage = xmldoc.CreateElement("message");
            eIMessage.InnerText = info;
            root.AppendChild(eIMessage);

            XmlElement eIInfo = xmldoc.CreateElement("info");
            eIInfo.InnerText = info;
            root.AppendChild(eIInfo);

            //保存创建好的XML文档
            //xmldoc.Save("c:\\data.xml");

            return xmldoc.InnerXml;
        }
        public static XmlDocument SortXmlNew(string xPath,string tagName)
        {
        	XmlDocument xmlDocCopy = new XmlDocument();
        	xmlDocCopy.LoadXml(xmlDocument.OuterXml);
            XmlNode XmlNodeCopy = xmlDocCopy.SelectSingleNode(xPath);
            
            XmlNodeList list = xmlDocCopy.SelectNodes(xPath+"//"+tagName);
            List<XmlNode> arrNode = new List<XmlNode>();
            foreach (XmlNode node in list)
            {            	
            	arrNode.Add(node);
            }
            XmlNodeCopy.RemoveAll(); //删除options
            // 关键bai：使用匿名方法，du按节点的Value排序zhi
            arrNode.Sort(delegate(XmlNode x, XmlNode y)
                         {
                         	//glb.MyDebugOut.DebugOut(x.Attributes["name"].Value);
                         	return x.Attributes["name"].Value.CompareTo(y.Attributes["name"].Value);
                         	//如果要降序排序，改成下dao面这句
                         	//return -x.Value.CompareTo(y.Value);
                         });
            XmlNode root=xmlDocCopy.SelectSingleNode(xPath);
            foreach(XmlNode xn in arrNode)
            {
            	root.AppendChild(xn);
            }
             
            //xmlDocCopy.Save("d:\\dt.xml");
        	return xmlDocCopy;
        }
    }

}